<script>
function rcoin() {
  return Math.random() < 0.5
}
let p = new Promise((resolve, reject) => {
  console.log('有一天，我和2108班出去野营，走到旷野')
  resolve('大家让史记去找水')
})
let p1 = p.then((v) => {
  console.log('然后', v)
  return '史记拿着水壶出发了'
})
let p21 = p.then(() => {})

let p2 = p1.then(v => {
  console.log('然后', v)
  const f = false
  console.log('判定：', f)
  if (f) {
    return '史记朝着大山走去'
  } else {
    throw '史记忽然想到一个水壶不够'
  }
})
let p3 = p2.then(v => {
  console.log('然后', v)
  console.log('越过了山')
  return '找到了水'
}, r => {
  console.error('错误', r)
  return '返回营地拿更大的容器'
})
let p4 = p3.then(v => {
  console.log('然后', v)
  if (v === '返回营地拿更大的容器') {
    return '拿了100升桶出发了'
  }
})
let p5 = p4.then((v) => {
  console.log('然后', v)
  return '史记朝着大海走去'
})
let p6 = p5.then((v) => {
  console.log('然后', v)
  throw '史记被外星人抓走了'
})
let p7 = p6.catch(r => {
  console.error('错误', r)
  return '派张尧救史记并且拿到桶继续打水'
})
let p8 = p7.then(v => {
  console.log('然后', v)
  return '张尧救了史记，两人找到了水顺利回来了'
})



new Promise((resolve, reject) => {
  reject(Math.random())
}).catch(value => {
  if (value>0.5) {
    return 1
  } else {
    throw 2
  }
}).then(
  value => console.log('then:', value)
).catch(
  value => console.log('catch', value))
</script>